package com.dd.cloud.user.service.chknowl.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.utils.ExtUtils;
import com.dd.cloud.user.entity.chknowl.DiseaseDiagnosisCode;
import com.dd.cloud.user.mapper.chknowl.DiseaseDiagnosisCodeMapper;
import com.dd.cloud.user.req.chknowl.DiseaseDiagnosisCodePageReq;
import com.dd.cloud.user.res.chknowl.DiseaseDiagnosisCodePageRes;
import com.dd.cloud.user.service.chknowl.IDiseaseDiagnosisCodeService;
import com.dd.cloud.user.vo.DiseaseDiagnosisCodeExcel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Lx
 * @version 1.0
 */
@Slf4j
@Service
public class DiseaseDiagnosisCodeServiceImpl extends ServiceImpl<DiseaseDiagnosisCodeMapper, DiseaseDiagnosisCode> implements IDiseaseDiagnosisCodeService {
    /**
     * 导入excel疾病诊断编码数据
     *
     * @param file
     */
    @Override
    public void importDiseaseDiagnosisCodeExcel(MultipartFile file) {
        if (file == null) {
            throw new ZekeException(400, "文件不能为空");
        }
        List<DiseaseDiagnosisCodeExcel> list;
        try {
            list = ExtUtils.importExcel(file, 0, 1, DiseaseDiagnosisCodeExcel.class);
        } catch (IOException e) {
            e.printStackTrace();
            throw new ZekeException(400, "导入错误");
        }
        UpdateWrapper<DiseaseDiagnosisCode> uw = new UpdateWrapper<>();
        uw.lambda().eq(DiseaseDiagnosisCode::getStatus, 1);
        uw.lambda().set(DiseaseDiagnosisCode::getStatus, -1);
        update(uw);
        List<DiseaseDiagnosisCode> saveList = new ArrayList<>(list.size());
        for (DiseaseDiagnosisCodeExcel item : list) {
            DiseaseDiagnosisCode code = new DiseaseDiagnosisCode();
            BeanUtils.copyProperties(item, code);
            code.setStatus(1);
            code.setCreateDate(LocalDateTime.now());
            saveList.add(code);
        }
        saveBatch(saveList);
    }

    /**
     * 查询中医诊断编码名称数据
     *
     * @param req
     * @return
     */
    @Override
    public IPage<DiseaseDiagnosisCodePageRes> queryDiseaseDiagnosisCodePage(DiseaseDiagnosisCodePageReq req) {
        IPage<DiseaseDiagnosisCodePageRes> iPage = req.pagePojo();
        iPage = getBaseMapper().queryDiseaseDiagnosisCodePage(iPage, req);
        return iPage;
    }
}
